<%@ page pageEncoding="UTF-8"%>

<%@ taglib uri="/tags/struts-tiles" prefix="tiles"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>OA管理系统-登录</title>
<link rel="stylesheet" href="resources/css/login.css" type="text/css" media="screen" />
<script type="text/javascript" src='resources/js/jquery-1.7.1.min.js'></script>
<script type="text/javascript" src='resources/js/app.util.js'></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/ServiceUser.js"></script> 
<script type="text/javascript" src="dwr/interface/WebAppUtil.js"></script> 

<!--[if lt IE 7]>
<script type="text/javascript" src="resources/js/pngfix/DD_belatedPNG_0.0.8a.js"></script>  
<script type="text/javascript">  
	$(function(){
		DD_belatedPNG.fix('.pngfix,img');
	});
</script>  
<![endif]-->
<script type="text/javascript">
	function frm_enter(event){ 
		e = event ? event :(window.event ? window.event : null); 
		if(e.keyCode==13){  
			$("#login_btn").trigger("click");
		}
	}
	$(function(){
		
		$("#username").keydown(frm_enter);
		$("#password").keydown(frm_enter);

		$("#login_btn").click(function(){
			
			var logon_action = $("#logon_action option").filter(":selected").attr("value");
			logon_action = logon_action || "dwr";
			
			var user_name = $.trim($("#username").val());
			var user_pwd = $("#password").val();
			
			if(user_name == ""){
				$("#username").focus();
				return;
			}

			if(user_pwd == ""){
				$("#password").focus();
				return;
			}
			
			if (logon_action == "dwr") {
				// DWR logon
				ServiceUser.getAccountByUserNameAndPwd(user_name, user_pwd, {
					callback: function(obj) {
						if(obj && obj.id){
							if (obj.userStatus == 0){
								$(".login_error").css("visibility", "visible");
								$(".login_error").text("登陆失败：该用户还未激活!");
							} else {
								$(".login_error").css("visibility", "hidden");
								// Session Storage
								WebAppUtil.injectEntityToSession({id: obj.id, userName: obj.userName, password: obj.password, userType: obj.userType, userStatus:obj.userStatus}, {
									callback: function(data){
										alert("登陆成功.");
										window.location.href = "admin/student.do?action=studentPageIndex";
									},
									timeout: 5000,
									errorHandler: function(message) {
										alert("用户Session存入失败:" + message);
									}
								});
							}
						}else{
							$(".login_error").css("visibility", "visible");
							$(".login_error").text("登陆失败：用户名或者密码不正确!");
						}
					},
					timeout: 5000,
					errorHandler: function(message) {
						$(".login_error").css("visibility", "visible");
						$(".login_error").text("登陆失败：" + message);
					}
				});
			} else if(logon_action == "ajax") {
				// logon with AJAX (non-DWR)
				generic_ajax_op("user.do?action=accountHomePageLogin", "{'do':'yes','ajax':'true','userName':'" + user_name + "','password':'" + user_pwd + "'}", null, (function(rsp_msg, callback_args){
					if (rsp_msg == "success") {
						alert("登陆成功");
						window.location.href = "admin/student.do?action=studentPageIndex";
					} else {
						$(".login_error").css("visibility", "visible");
						$(".login_error").text(rsp_msg);
					}
				}));
			} else if(logon_action == "nonajax") {
				// classic logon without AJAX, only form submit executed.
				$("#login_frm").submit();
			}
		});

		$("#reset_btn").click(function(){
			$("#username").val("");
			$("#password").val("");
		});
		
		if($.browser.msie){
			$("img").attr("align","middle"); 
		}

	});
</script>
</head>
<body>
	<div class="wrapper">
		<div class='login_logo'><img src="resources/images/layout/login/logo.png" /></div>
		<div class="login_wrapper">
			<div class="login_pannel pngfix">
				<br style="line-height:0;"/>
				<div class="login_frm" style="margin-top: 40px;">
					<form action="user.do?action=accountHomePageLogin" method="post" id="login_frm">						
						<div class="login_error" <logic:notPresent name="msg">style="visibility:hidden"</logic:notPresent>>
							<logic:present name="msg">${msg}</logic:present>
						</div>
						<div class="login_input">
							<span>用户</span>
							<input type="text" id="username" name="userName" />
						</div>
						<div class="login_input">
							<span>密码</span>
							<input type="password" id="password" name="password" />
						</div>
						<div>
							<span>类型</span>
							<select id="logon_action" style="margin-left: 10px; vertical-align: middle;">
								<option value="nonajax">传统形式登陆</option>
								<option value="ajax">传统Ajax登陆</option>
								<option value="dwr" selected="selected">DWR实现登陆</option>
							</select>
						</div>
						<div class="login_btn">
							<button type="button" id="login_btn" />登录</button>
							<button type="button" id="reset_btn" />重置</button>
						</div>
						<input type="hidden" name="do" />
					</form>
				</div>
			</div>
		</div>
		<div class="site_desc">- OA管理系统(DWR实现) -</div>
	</div>
</body>
</html>